package com.wecloud.core.action.bussiness;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.wecloud.core.action.impl.BaseTransAction;
import com.wecloud.core.constant.ErrorMsg;
import com.wecloud.core.constant.WeContext;
import com.wecloud.core.data.jnl.JnlService;
import com.wecloud.core.exception.WeException;
import com.wecloud.core.user.WeUser;
import com.wecloud.redis.service.RedisService;

/**
 * @author qq374
 *
 */
public class QueryUserIsFirstLoginAction extends BaseTransAction{
	@Autowired
	private JnlService jnlService;//mysql
	@Autowired
	private RedisService redisService;//redis
	
	@Override
	public void excute(WeContext context)  throws WeException{
		String userName = context.getString("userName");
		Map userMap = jnlService.queryForObject("user.userIsfirstLogin", userName);
		Map r = new HashMap();
		r.put("FirstLogin",true);
		r.put("userStatus","0");
		if(userMap != null){
			if("1".equals(userMap.get("userStatus"))){
				r.put("FirstLogin",false);
				r.put("userStatus","1");
			}
			List<Map> list = redisService.keys();
			Iterator it = list.iterator();
			while(it.hasNext()){
				String sessionId = it.next().toString();
				WeUser user = redisService.get(sessionId);
				if(userName.equals(user.getUserName())){
					r.put("JSESSIONID", sessionId);
					break ;
				}
			}
		}
		context.setData("content", r);
	}
}
